<?php

class usermanagement extends Controller{

	function index(){
		if(!$this->user->isLogged()){
			$this->response->redirect(MAIN_PAGE);
		}
		$this->data["user_list"]=$this->user->loaduser()->rows;
		$this->document->setTitle("Document Management Solutions");
		$this->document->setSelectedMenu("menu_setting");
		$this->children=array("header","footer");
		$this->template="user.html";
		$this->response->setOutput($this->render(TRUE),0);
	}

	function userpost(){
		$jsonarr=array();
		if($this->request->is_ajax_request() && $this->user->isLogged()){
			if($this->db->escape($_POST["userid"])==""){
				if($this->user->CheckUserExist($this->db->escape($_POST["uname"]))==0){
					$jsonarr["user_id"]=$this->user->userinsert();
					$jsonarr["msg"]="User Added successfully";
					$jsonarr["status"]=true;
				}
				else{
					$jsonarr["msg"]="Username already exists,Please try again with different name";
					$jsonarr["status"]=false;
				}
			}
			else{
				$this->user->updateuser();
				$jsonarr["msg"]="User updated successfully";
				$jsonarr["status"]=true;
			}
		}
		else{
			$jsonarr["status"]=false;
			$jsonarr["redirect"]=true;
		}
		echo json_encode($jsonarr);
	}

	function edit(){
		$jsonarr=array();
		if($this->request->is_ajax_request() && $this->user->isLogged()){
			$res=$this->user->getuserdetailsbyuserid($this->db->escape($_POST["userid"]))->rows;
			if(count($res[0])>0){
				$res[0]["status"]=true;
				echo json_encode($res[0]);
			}
			else{
				echo json_encode(array("status"=>false));
			}
		}
		else{
			echo json_encode($jsonarr);
		}

	}

	function delete(){
		$jsonarr=array();
		if($this->request->is_ajax_request() && $this->user->isLogged()){
			$this->user->deleteuser($this->db->escape($_POST["userid"]));
			$jsonarr["status"]=true;
		}
		else{
			$jsonarr["status"]=false;
			$jsonarr["redirect"]=true;
		}
	 echo json_encode($jsonarr);
	}


}

?>